﻿using System;
using System.Collections.Generic;
using System.Xml;
using EditorExtensions;
using UnityEngine;

namespace EditorFrameWork
{
    public class XMLGUI
    {
        private List<XMLGUIBase> m_GUIBases = new List<XMLGUIBase>();
        public Dictionary<string, XMLGUIBase> GUIBasesForID = new Dictionary<string, XMLGUIBase>();

        public void Draw()
        {
            foreach (var guiBase in m_GUIBases)
            {
                guiBase.Draw();
            }
        }

        public T GetGUIBaseById<T>(string id) where T : XMLGUIBase
        {
            if (GUIBasesForID.TryGetValue(id, out var t))
            {
                return t as T;
            }
            else
            {
                return default;
            }
        }

        private Dictionary<string, Func<XMLGUIBase>> mFactoryForGUIBaseNames = new Dictionary<string, Func<XMLGUIBase>>()
        {
            {
                "Label", () => (new XMLGUILabel())
            },
            {
                "TextField", () => (new XMLGUITextField())
            },
            {
                "TextArea", () => (new XMLGUITextArea())
            },
            {
                "Button", () => (new XMLGUIButton())
            },
            {
                "LayoutLabel", () => (new XMLGUILayoutLabel())
            },
            {
                "LayoutButton", () => (new XMLGUILayoutButton())
            },
            {
                "LayoutHorizontal", () => (new XMLGUILayoutHorizontal())
            },
            {
                "LayoutVertical", () => (new XMLGUILayoutVertical())
            },
        };

        public void ReadXML(string xml)
        {
            var doc = new XmlDocument();
            doc.LoadXml(xml);
            var rootNode = doc.SelectSingleNode("GUI");
            if (rootNode != null)
            {
                ChildElements2GUIBase(rootNode as XmlElement, this);
            }
        }

        public void ChildElements2GUIBase(XmlElement rootElement, XMLGUI rootXMLGUI)
        {
            XMLGUIBase guibase = default;
            foreach (XmlElement rootNodeChildNode in rootElement.ChildNodes)
            {
                mFactoryForGUIBaseNames.TryGetValue(rootNodeChildNode.Name, out var xmlGUIBaseFactory);
                {
                    guibase = xmlGUIBaseFactory?.Invoke();
                    guibase.ParseXML(rootNodeChildNode, rootXMLGUI);
                    AddGUIBase(guibase, rootXMLGUI);
                }
            }
        }

        void AddGUIBase(XMLGUIBase xmlguiBase, XMLGUI rootXMLGUI)
        {
            m_GUIBases.Add(xmlguiBase);
            if (!string.IsNullOrEmpty(xmlguiBase.Id))
            {
                GUIBasesForID.Add(xmlguiBase.Id, xmlguiBase);
                if (rootXMLGUI == this)
                {
                    
                }
                else
                {
                    rootXMLGUI.GUIBasesForID.Add(xmlguiBase.Id, xmlguiBase);
                }
            }
        }
    }
}